var address='http://' + window.location.host,noImg = 'img/normal.png';
FastClick.attach(document.body);
function GetUrlString(name){
  var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  var s=window.location.search.substr(1).match(reg);
  var h = window.location.hash.substr(1).match(reg);
  if(s){return unescape(s[2]);}
  if(h){return unescape(h[2]);}
  return "";
};

//幻灯片
function slide(ele){
  var $that = ele;
  slength = $that.find('li').length,
      $item = $that.find(".point span");
  $item.eq(0).addClass("active");
  var slider = Swipe(document.getElementById('slide'), {
    auto: 3000,
    callback:function(index) {
      var index = index;
      index = index > slength-1 ? index -slength : index;
      index = index < 0 ? 0 :index;
      $item.eq(index).addClass("active").siblings().removeClass('active');
    }
  });
};

~function start(){
  $(document).on("click",".starAble input",function(){
      $(this).addClass("checked").siblings("input").removeClass("checked");
  })
}();

// 新房列表
function newlistTPL(data){
  var data = data.list,html ='',point = localStorage.getItem("point");
  if(point){
    point = point.split(",");
    var lat1 = Number(point[0]),lng1 = Number(point[1]);
  }
  $.each(data,function(index,info){
    var star = info.houseEvaluationStar,lat2 =info.latitude,lng2=info.longitude,
    distance = getDistance(lat1,lng1,lat2,lng2);
    distance = (distance/1000).toFixed(1);
    var img = info.image ? info.image+'&size=180x150' : noImg ;
    html += '<a class="db list p5 b-b" href="newList-detail.html?id='+info.id+'">'+
        '<dl>'+
          '<dt><img src="'+img+'" class="pic"></dt>'+
          '<dd>'+
            '<div class="name l-h20 black">'+info.name+'</div>'+
            '<div class="comment">'+
              '<span class="fr">'+info.averagePrice+'元<span class="m-lr3">&frasl;</span>平</span>'+
              '<div class="start fl">';
                for(var s=5;s>0;s--){
                  if(s==star){
                    html +='<input type="radio" value="'+s+'" class="checked" disabled>';
                  }else{
                     html +='<input type="radio" value="'+s+'" disabled>';
                  }
                }
              html +='</div>'+
              '<span class="gray m-lr5">'+info.houseEvaluationNumber+'条点评</span>'+
            '</div>'+
            '<p class="listAddress"><span class="fr gray"><i class="icon-gps"></i>'+distance+'km</span>'+info.address+'</p>'+
            '<div class="label orange">';
            var label = info.labelList;
            for(var i=0;i<label.length;i++){
              html+='<span>'+label[i]+'</span>';
            }
            html += '</div>'+
          '</dd>'+
        '</dl>'+
        '<p class="school rel">'+info.traffic+'</p>'+
    '</a>';
  })
  return html;
}


$(".coverDig").click(function(){
  $(".coverDig").hide();
  $(".menu-item").hide();
  $(".menu").find(".icon-down").removeClass("checked");
})
$(".coverBox").click(function(e){
  e.stopPropagation();
  $(".coverDig").show();
})
$(".tipsTool .cancel").click(function(e){
  e.stopPropagation();
  $(".coverDig").hide();
})

// 计算经纬度距离
var EARTH_RADIUS = 6378137.0;
var PI = Math.PI;
function getRad(d){
    return d*PI/180.0;
}
function getDistance(lat1,lng1,lat2,lng2){
    var f = getRad((lat1 + lat2)/2),
    g = getRad((lat1 - lat2)/2),
    l = getRad((lng1 - lng2)/2),
    sg = Math.sin(g),
    sl = Math.sin(l),
    sf = Math.sin(f),
    s,c,w,r,d,h1,h2;
    a = EARTH_RADIUS,
    fl = 1/298.257;
    sg = sg*sg;
    sl = sl*sl;
    sf = sf*sf;
    
    s = sg*(1-sl) + (1-sf)*sl;
    c = (1-sg)*(1-sl) + sf*sl;
    
    w = Math.atan(Math.sqrt(s/c));
    r = Math.sqrt(s*c)/w;
    d = 2*w*a;
    h1 = (3*r -1)/2/c;
    h2 = (3*r +1)/2/s;
    return d*(1 + fl*(h1*sf*(1-sg) - h2*(1-sf)*sg));
}

function message(str){
  var $msg = $(".messageDig");
  if($msg.length>0){
    $msg.html(str).show();
  }else{
    var html = '<div class="messageDig">'+str+'</div>';
    $("body").append(html);
    $(".messageDig").show();
  }
  setTimeout(function(){
    $(".messageDig").hide();
  },2000);
};

//邮箱格式验证
function checkemail(userEmail){
    var str = userEmail;
    var reg = /^[-_a-zA-Z0-9]+(\.[-_a-zA-Z0-9]+)*@[-_a-zA-Z0-9]+(\.[-_a-zA-Z0-9]+)+$/; 
    if(reg.test(str)){ 
      return true;
    }else{ 
      return false;
    } 
};
//手机号码验证
 function checkphone(userPhone){
    var phone = userPhone;
    var rule = /^1[3,4,5,7,8,9]\d{9}$/; 
    if(rule.test(phone)){ 
        return true;
    }else{ 
        return false;
    } 
 };
 //电话号码验证
 function checktel(userPhone){
    var tel = userPhone;
    var rule = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/; 
    if(rule.test(tel)){ 
        return true;
    }else{ 
        return false;
    } 
 };
 //纯英文字母验证
 function checkenglish(num){
    var num = num;
    var rule = /^\w*[a-zA-Z]+\w*$/; 
    if(rule.test(num)){
      return true;
    }else{
      return false;
    }
 }
 //纯数字验证
 function checknumber(num){
    var num = num;
    var rule = /^[0-9]*$/; 
    if(rule.test(num)){
      return true;
    }else{
      return false;
    }
 }
  //身份证验证
 function checkidentityId(num){
    var num = num;
    var rule = /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/; 
    if(rule.test(num)){
      return true;
    }else{
      return false;
    }
 }
function checkNull(ele,msg){
  var notNull = true,msg = msg ? msg :"输入框不能为空";
  $.each(ele,function(index,that){
      if(that["value"]==""){
        notNull = false;
        $(this).addClass("errorTips");
      }else{
        $(this).removeClass("errorTips");
      }
  })
  if(notNull==false) message(msg);
  return notNull;
}
//表单验证 字段phone、email、number、english
function checkForm(ele){
    var pass = true;
    var value = ele.val();
    var eleType = ele.attr("data-type");
    switch(eleType){
      case "tel":
        pass = checktel(value) ? true : false;
        if(pass==false) message("请输入正确的固话号码");
        break;
      case "phone":
        pass = checkphone(value) ? true : false;
        if(pass==false) message("请输入正确的手机号");
        break;
      case "email":
        pass = checkemail(value) ? true : false;
        if(pass==false) message("请输入正确的邮箱地址");
        break;
      case "number":
        pass = checknumber(value) ? true : false;
        if(pass==false) message("只能输入数字");
        break;
      case "english":
        pass = checkenglish(value) ? true : false;
        if(pass==false) message("只能输入英文字母");
        break;
      case "identityId":
        pass = checkidentityId(value) ? true : false;
        if(pass==false) message("请输入正确的身份证");
        break;
    }
    return pass;
}

//选择图片并预览
$(".upLoadImg").on("change",function(){
    var $this = $(this),files,img,fr,imgType=/image.*/;
    if(window.FileReader){
      files = this.files;
      for(var i=0;i<files.length;i++){
        if(!files[i].type.match(imgType)){
          message("只能上传图片！");
          break;
        }
        if(files[i].size>1024*1024*10){
          message("图片大小超过限制");
          break;
        }
        img = document.createElement("img");
        img.file = files[i];
        fr = new FileReader();
        fr.onload = function(e){
          img.src = e.target.result;
          $this.siblings(".imgBox").empty().append(img);
        }
        fr.readAsDataURL(img.file);
      }
    }
});

// 底部菜单
$(".bar").on("click","li",function(){
    var $this = $(this);
    $this.siblings().removeClass("active");
    if($this.hasClass("active")){
      $this.removeClass("active")
    }else{
      $this.addClass("active")
    }
})

$("#houseImg").on("click","img",function(){
    var src = $(this).attr("src");
    if($(".bigImg").length>0){
        $(".bigImg").attr("src",src).show();
    }else{
        var img = '<img src="'+src+'" class="bigImg">';
        $("body").append(img).show();
    }

    if($(".coverImg").length>0){
        $(".coverImg").show();
    }else{
        $("body").append('<div class="coverImg"></div>').show();
    }    
})
$(document).on("click",".coverImg",function(){
    $(".coverImg").hide();
    $(".bigImg").hide();
})

// 获取时间
 function DateFun(){}
 DateFun.prototype.currentTime = function(){
    var date = new Date(),
      y = date.getFullYear(),
      m = date.getMonth()+1,
      d = date.getDate(),
      h = date.getHours(),
      s = date.getMinutes(),
      x = date.getSeconds();
      m < 10 ? m = "0"+m : m;
      d < 10 ? d = "0"+d : d;
      h < 10 ? h = "0"+h : h;
      s < 10 ? s = "0"+s : s;
      x < 10 ? x = "0"+x : x;
    var outTime = y +"-"+ m +"-"+ d +" "+ h +":"+s+":"+x;
    return outTime;
 };

  DateFun.prototype.wrongDay = function(str1,str2){
  if(str1.length<12){
    var y1=str1.split("-")[0];
    var m1=str1.split("-")[1];
    var d1=str1.split("-")[2];
    var y2=str2.split("-")[0];
    var m2=str2.split("-")[1];
    var d2=str2.split("-")[2];

    if(y2<y1){
      return true;
    }else if(y1==y2){
      if(m1>m2){
        return true;
      }else if(m1==m2){
        if(day1>day2){
          return true;
        }
      }
    }
  }
}
